<?php
/*
 * @Description:
 * @Author: 橘子味的心
 * @Date: 2020-11-13 07:11:07
 * @LastEditTime: 2021-04-01 20:10:19
 * @LastEditors: 橘子味的心
 * @Reference:
 */

declare(strict_types=1);

namespace app\system\model;

use think\Model;

class Config extends Model
{
    /**
     * 根据条件获取配置列表信息
     */
    public function getAll($map, $limit, $od)
    {
        $data = $this->where($map)->append(['type_text','group_text','image'])->order($od)->paginate($limit);
        return $data;
    }
    /**
     * 验证配置标识的唯一性
     */
    public function check($name, $uid)
    {
        if ($uid != '') {
            $uname = $this->where('id', $uid)->value('name');
            if ($uname == $name) {
                return tocode(6, 'true');
            }
        }
        $result = $this->where('name', $name)->find();
        if ($result) {
            return tocode(5, 'false');
        } else {
            return tocode(6, 'true');
        }
    }
    /**
     * 添加/编辑信息
     */
    public function info($param)
    {
        if (!empty($param['id'])) {
            $res = $this->find($param['id']);
            $msg = '编辑';
        } else {
            $res = $this;
            $msg = '添加';
        }
        if ($res->save($param) == false) {
            return tocode(100, $msg . '失败');
        }
        cache('db_config_data', null);
        return tocode(200, $msg . '成功');
    }
    /**
     * 根据id获取配置信息
     * @param $id
     */
    public function getOne($id)
    {
        $res = $this->where('id',$id)->append(['image'])->find();
        return tocode(200, 'ok', $res);
    }

    /**
     * [State 配置状态]
     */
    public function status($id, $status)
    {
        try {
            $this->where('id', $id)->update(['status' => $status]);
        } catch (\Exception $e) {
            return false;
        }
        return true;
    }

    /**
     * configSave 保存配置
     * @param $config
     * @return array
     */
    public function configSave($config)
    {
        $this->startTrans(); // 启动事务
        try {
            if ($config && is_array($config)) {
                foreach ($config as $name => $value) {
                    if ($name == "list_rows") {
                        if ($value <= 0) {
                            $value = 1;
                        }
                    }
                    $map = array('name' => $name);
                    $this->where($map)->update(['value' => $value]);
                }
            }
            $this->commit(); // 提交事务
            cache('db_config_data', null);
            return tocode(200, '配置保存成功');
        } catch (\Exception $e) {
            $this->rollback(); // 回滚事务
            return tocode(100, '配置保存失败');
        }
    }
    public function handleAll($param, $type = 0)
    {
        try {
            //判断是否是批量删除
            if ($type == 2) {
                $this::destroy(explode(',', $param));
            } else {
                $this->where('id', 'in', $param)->update(['status' => $type]);
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
    public function getTypeTextAttr($val,$data)
    {
        return config_group('config_type_list')[$data['type']];
    }
    public function getGroupTextAttr($val,$data)
    {
        return config_group('config_group_list')[$data['group']];
    }
    public function getExtraAttr($val,$data)
    {
        if ($data['type'] == 4) {
            $v = empty($val) ? '' : parse_config_attr($val);
            return $v;
        }
    }
    public function getImageAttr($val,$data)
    {
        if($data['type'] == 6){
            return empty($data['value']) ? '' : _sImage($data['value']);
        }
    }
}
